using System;
using UnityEngine;
using QFramework;
using UnityEngine.Serialization;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace Survivor
{
	public partial class Player : ViewController
	{
		public static Player Instance;
		
		
		public float MovementSpeed = 5f;
		
		void Start()
		{
			Instance = this;
			HurtBox.OnTriggerEnter2DEvent(col =>
			{
				var hitBox = col.GetComponent<HitBox>();
				if (hitBox && hitBox.CompareTag("Enemy"))
				{
					Global.Hp.Value--;
					if (Global.Hp.Value <= 0)
					{
						AudioKit.PlaySound("Die");
						gameObject.DestroySelfGracefully();
						UIKit.OpenPanel<UIGameOverPanel>();	
					}
					else
					{
						AudioKit.PlaySound("Hurt");
					}
						
				}
			}).UnRegisterWhenGameObjectDestroyed(this);
		}

		private void Update()
		{
			float inputX = Input.GetAxisRaw("Horizontal");
			float inputY = Input.GetAxisRaw("Vertical");
			Vector2 targetVelocity = new Vector2(inputX, inputY).normalized * MovementSpeed;

			PlayerRB.velocity = Vector2.Lerp(PlayerRB.velocity, targetVelocity, 1.0f - Mathf.Exp(-Time.deltaTime * 5f));
		}

		private void OnDestroy()
		{
			Instance = null;
		}
	}
}
